const app = getApp()
import http from "../../utils/request.js";
const publicFun = require('../../utils/public');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        background: [
            '/media/banner/1.webp',
            '/media/banner/2.webp',
            '/media/banner/3.webp',
            '/media/banner/4.webp',
            '/media/banner/5.webp',
        ],
        interval: 2000,
        duration: 500,
        dreamList: [],
        currPage: 1,
        searchWord: '', //搜索关键词
        loading: false,
        vip: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            vip: app.globalData.vip
        })
        if (app.globalData.sysUserInfo.nickName == '微信用户') {
            wx.showModal({
                title: '提示',
                content: '完善个人头像和昵称等信息？',
                success(res) {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/page/personInfo/personInfo',
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    },

    //查询梦境
    getDream(clear) {
        this.setData({
            loading: clear ? true : false,
            dreamList: clear ? [] : this.data.dreamList
        })
        if (clear) {
            this.setData({
                currPage: 1
            })
        }
        let data = {
            dreamAuth: 2,
            pageSize: 10,
            currPage: this.data.currPage,
            searchWord: this.data.searchWord,
            currUserId: app.globalData.userId,
            tagIds: ''
        }
        http.Request({
            url: 'getDream',
            method: 'GET',
            data: data
        }).then(re => {
            if (re.code == 200) {
                this.setData({
                    dreamList: clear ? re.data : this.data.dreamList.concat(re.data),
                    loading: false
                });
            }
        });
    },

    // 搜索
    toSearch(e) {
        this.setData({
            searchWord: e.detail.value
        })
        this.getDream(1)
    },

    //跳转到梦境详情
    toDetail(e) {
        wx.navigateTo({
            url: '/page/dreamDetail/dreamDetail?id=' + e.currentTarget.dataset.id,
        })
    },

    // 点赞梦境
    loveDream(e) {
        let data = {
            id: e.currentTarget.dataset.id,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'loveDream',
            method: 'POST',
            data: data
        }).then(re => {
            if (re.code == 200) {
                this.getDream(1)
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }
        this.getDream(1); //获取梦境
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getDream(1); //获取梦境
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.currPage++
        this.setData({
            currPage: this.data.currPage
        })
        this.getDream(); //获取梦境
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '梦萦记',
            path: 'page/index/index'
        }
    }
})